﻿using Microsoft.AspNetCore.Http;
using Org.BouncyCastle.Asn1.Ocsp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace com.iot.core.util
{
    public class RequestUtil
    {
        public  static string GetClientIP(HttpContext context)
        {
            try
            {
                var ip4 = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
                if (string.IsNullOrEmpty(ip4))
                {
                    ip4 = context.Request.Headers["REMOTE_ADDR"].FirstOrDefault();
                }
                if (string.IsNullOrEmpty(ip4))
                {
                    ip4 = context.Connection.RemoteIpAddress?.MapToIPv4().ToString();
                }

                return ip4 ?? "未知";
            }
            catch 
            {
                return "未知";
            }
             
        }
        public static string GetBrowserName(HttpContext context) 
        {
            try
            {
                if (!context.Request.Headers.ContainsKey("User-Agent"))
                {
                    return "";
                }
                var userAgent = context.Request.Headers["User-Agent"].FirstOrDefault() ?? "";
                string browserName = "";
                var ver = "";

                string fullBrowserName = string.Empty;
                browserName = string.Empty;
                ver = string.Empty;
                // IE
                string regexStr = @"msie (?<ver>[\d.]+)";
                Regex r = new Regex(regexStr, RegexOptions.IgnoreCase);
                Match m = r.Match(userAgent);
                if (m.Success)
                {
                    browserName = "IE";
                    ver = m.Groups["ver"].Value;
                    fullBrowserName = string.Format("{0} {1}", browserName, ver);
                    return fullBrowserName;
                }
                // Firefox
                regexStr = @"firefox\/([\d.]+)";
                r = new Regex(regexStr, RegexOptions.IgnoreCase);
                m = r.Match(userAgent);
                if (m.Success)
                {
                    browserName = "firefox";
                    ver = m.Groups["ver"].Value;
                    fullBrowserName = string.Format("{0} {1}", browserName, ver);
                    return fullBrowserName;
                }
                // Chrome
                regexStr = @"chrome\/([\d.]+)";
                r = new Regex(regexStr, RegexOptions.IgnoreCase);
                m = r.Match(userAgent);
                if (m.Success)
                {
                    browserName = "chrome";
                    ver = m.Groups["ver"].Value;
                    fullBrowserName = string.Format("{0} {1}", browserName, ver);
                    return fullBrowserName;
                }
                // Opera
                regexStr = @"opera.([\d.]+)";
                r = new Regex(regexStr, RegexOptions.IgnoreCase);
                m = r.Match(userAgent);
                if (m.Success)
                {
                    browserName = "opera";
                    ver = m.Groups["ver"].Value;
                    fullBrowserName = string.Format("{0} {1}", browserName, ver);
                    return fullBrowserName;
                }
                // Safari
                regexStr = @"version\/([\d.]+).*safari";
                r = new Regex(regexStr, RegexOptions.IgnoreCase);
                m = r.Match(userAgent);
                if (m.Success)
                {
                    browserName = "Safari";
                    ver = m.Groups["ver"].Value;
                    fullBrowserName = string.Format("{0} {1}", browserName, ver);
                    return fullBrowserName;
                }
                if (fullBrowserName.Length > 50)
                {
                    fullBrowserName = fullBrowserName.Substring(0, 40);
                }
                return fullBrowserName ?? "";
            }
            catch
            {
                return "未知";
            }
            
        }
        public static string GetOS(HttpContext context)
        {
            try
            {
                if (!context.Request.Headers.ContainsKey("User-Agent"))
                {
                    return "未知";
                }
                string userAgent = context.Request.Headers["User-Agent"].FirstOrDefault() ?? "";
                string osVersion = "";

                if (userAgent.Contains("NT 10.0"))
                {
                    osVersion = "Win10";
                }
                else if (userAgent.Contains("NT 6.3"))
                {
                    osVersion = "Win8";
                }
                else if (userAgent.Contains("NT 6.2"))
                {
                    osVersion = "Win8";
                }
                else if (userAgent.Contains("NT 6.1"))
                {
                    osVersion = "Win7";
                }
                else if (userAgent.Contains("NT 6.1"))
                {
                    osVersion = "Win7";
                }
                else if (userAgent.Contains("NT 6.0"))
                {
                    osVersion = "Win2008";
                }
                else if (userAgent.Contains("NT 5.2"))
                {
                    if (userAgent.Contains("64"))
                        osVersion = "WinXP";
                    else
                        osVersion = "Win2003";
                }
                else if (userAgent.Contains("NT 5.1"))
                {
                    osVersion = "WinXP";
                }
                else if (userAgent.Contains("NT 5"))
                {
                    osVersion = "Win2000";
                }
                else if (userAgent.Contains("NT 4"))
                {
                    osVersion = "WinNT4";
                }
                else if (userAgent.Contains("Mac"))
                {
                    osVersion = "Mac";
                }
                else if (userAgent.Contains("Unix"))
                {
                    osVersion = "UNIX";
                }
                else if (userAgent.Contains("Linux"))
                {
                    osVersion = "Linux";
                }
                else if (userAgent.Contains("SunOS"))
                {
                    osVersion = "SunOS";
                }
                else
                {
                    osVersion = "未知";
                }
                return osVersion;
            }
            catch
            {
                return "未知";
            } 
        }


        public static string GetHttpPrev(HttpContext context)
        {
            var Request = context.Request;
            
            if (Request.Headers.ContainsKey("HostPrev"))
            {
                return Request.Headers["HostPrev"].FirstOrDefault()??"";
            }
            string http = Request.Scheme;
            string host = Request.Host.Host;
            string port = Request.Host.Port.ToString();
            var r = string.Format("{0}://{1}:{2}", http, host, port);
            return r;
        }
        
    }
}
